package org.eae.eldf.dao.sys.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.googlecode.ehcache.annotations.TriggersRemove;

import org.eae.eldf.dao.sys.ISysOperatorDao;
import org.eae.eldf.dto.sys.OperatorRoleCheckDto;
import org.eae.eldf.dto.sys.SysOperatorQueryDto;
import org.eae.eldf.entity.sys.SysOperator;
import org.eae.eldf.entity.sys.SysPurview;
import org.eae.eldf.entity.sys.SysRole;
import org.eae.eldf.framework.dao.JdbcDaoImpl;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.framework.persistence.Criteria;


@Repository
public class SysOperatorDaoImpl extends JdbcDaoImpl implements ISysOperatorDao{
	
	
	public SysOperator findOperatorByLoginName(String loginname){
		Criteria c = new Criteria(SysOperator.class);
		c.and("susername", new String[]{loginname});
		return this.querySingleResult(c);
	}

	@Override
	public List<SysPurview> findUserAuthCodes(String username) {
		StringBuffer sql = new StringBuffer("SELECT P.SURLPATH,P.PARENTID FROM SYS_PURVIEW P ")
		.append("LEFT JOIN SYS_ROLE_PURVIEW RR ON P.SYS_PURVIEW_ID=RR.SYS_PURVIEW_ID ")
		.append("LEFT JOIN SYS_ROLE R ON RR.SYS_ROLE_ID = R.SYS_ROLE_ID ")
		.append("LEFT JOIN SYS_OPERATOR_ROLE OPR ON R.SYS_ROLE_ID=OPR.SYS_ROLE_ID ")
		.append("LEFT JOIN SYS_OPERATOR OP ON OPR.SYS_OPERATOR_ID=OP.SYS_OPERATOR_ID ")
		.append("WHERE OP.SUSERNAME=?");
		return this.queryBySQL(sql.toString(), new String[]{username}, SysPurview.class);
	}

	@Override
	public Pagination<SysOperator> pageUserList(Pagination<SysOperator> pager,
			SysOperatorQueryDto dto) {
		StringBuffer sql = new StringBuffer("SELECT * FROM SYS_OPERATOR T WHERE T.BISDELETE=0 ");
		List<Object> params = new ArrayList<Object>();
		if(null!=dto){
			if(StringUtils.isNotBlank(dto.getSusername())){
				sql.append("AND T.SUSERNAME LIKE ? ");
				params.add("%"+dto.getSusername()+"%");
			}
			if(StringUtils.isNotBlank(dto.getSrealname())){
				sql.append("AND T.SREALNAME LIKE ? ");
				params.add("%"+dto.getSrealname()+"%");
			}
			if(StringUtils.isNotBlank(dto.getSmobile())){
				sql.append("AND T.SMOBILE LIKE ? ");
				params.add("%"+dto.getSmobile()+"%");
			}
			if(StringUtils.isNotBlank(dto.getSmail())){
				sql.append("AND T.SMAIL LIKE ? ");
				params.add("%"+dto.getSmail()+"%");
			}
			if(StringUtils.isNotBlank(pager.getOrderBy()) && StringUtils.isNotBlank(pager.getSort())){
				sql.append(" ORDER BY "+pager.getOrderBy().toUpperCase()+" "+pager.getSort().toUpperCase());
			}else{
				sql.append(" ORDER BY DCREATETIME DESC");
			}
		}
		
		return this.queryListByPage(pager, sql.toString(), params, SysOperator.class);
	}

	@Override
	public List<OperatorRoleCheckDto> findRoleByUser(Long operatorId) {
		if(operatorId!=null){
			StringBuffer sql = new StringBuffer("select * from (SELECT '1' AS NOCHECK,R.SYS_ROLE_ID,R.SROLENAME,R.SREMARK,R.DLASTUPDATETIME FROM SYS_ROLE R ")
					.append("WHERE R.SYS_ROLE_ID NOT IN(SELECT T.SYS_ROLE_ID FROM SYS_OPERATOR_ROLE T ")
					.append("LEFT JOIN SYS_ROLE R ON T.SYS_ROLE_ID = R.SYS_ROLE_ID WHERE T.SYS_OPERATOR_ID=?) ")
					.append("UNION ALL ")
					.append("SELECT '0' AS NOCHECK,T.SYS_ROLE_ID,R.SROLENAME,R.SREMARK,R.DLASTUPDATETIME FROM SYS_OPERATOR_ROLE T ")
					.append("LEFT JOIN SYS_ROLE R ON T.SYS_ROLE_ID = R.SYS_ROLE_ID WHERE T.SYS_OPERATOR_ID=?)t1 order by t1.DLASTUPDATETIME desc");
					return this.queryBySQL(sql.toString(), new Object[]{operatorId,operatorId},OperatorRoleCheckDto.class);
		}else{
			StringBuffer sql = new StringBuffer("SELECT '1' AS NOCHECK,R.SYS_ROLE_ID,R.SROLENAME,R.SREMARK FROM SYS_ROLE R order by dlastupdatetime desc");
			return this.queryBySQL(sql.toString(),OperatorRoleCheckDto.class);
		}
		
	}

	
	
}
